package com.mop.sg2.map;

import java.util.Iterator;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

import com.mop.sg2.layer.LayerManager;

public class BackGroupView extends SurfaceView implements Callback,
		OnTouchListener, Runnable {

	private SurfaceHolder holder;

	private int status = 0;

	public void setStatus(int status) {
		this.status = status;
	}

	private int frameCount = 0;

	private ImageBean selected = null;

	private boolean flag = true;

	private boolean isViewOn = false;

	private LevelUp levelUpView;

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	Context context;

	public BackGroupView(Context context) {
		super(context);
		this.context = context;
		Log.i("TAG", "BackGroupView()");
		holder = this.getHolder();
		getHolder().addCallback(this);
		this.setOnTouchListener(this);
	}

	public void onDraw(Canvas canvas) {

		try {

			if (frameCount++ < 2 || status == 0) {
				reDraw(canvas);
				selected = null;
			}


		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			// this.isViewOn = false;
		}
	}

	public void reDraw(Canvas canvas) {
		// 绘制背景
		ImageBean tmp = TestMap.maps.get("FullBackGourd");
		{
			int g_width = canvas.getWidth();
			int g_height = canvas.getHeight();
			int index_w = 0;
			int index_h = 0;

			while (true) {
				while (true) {
					canvas.drawBitmap(tmp.getBitmap(), index_w, index_h, null);
					// 向右延伸
					if (index_w > g_width) {
						break;
					} else {
						index_w = index_w + tmp.getWidth();
					}
				}

				// 向下延伸
				if (index_h > g_height) {
					break;
				} else {
					index_h = index_h + tmp.getHeigth();
					index_w = 0;
				}
			}
		}

		Iterator<ImageBean> itr = TestMap.maps.values().iterator();
		Paint textPain = new Paint();
		textPain.setColor(Color.RED);

		float x_tmp;
		while (itr.hasNext()) {
			tmp = itr.next();
			if (!tmp.getName().equals("FullBackGourd")) {
				canvas.drawBitmap(tmp.getBitmap(), tmp.getX(), tmp.getY(), null);

				float width = textPain.measureText(tmp.getName());

				if (tmp.getWidth() > width) {
					x_tmp = tmp.getX() + (tmp.getWidth() - width) / 2;
				} else {
					x_tmp = tmp.getX();
				}
				canvas.drawText(tmp.getName(), x_tmp, tmp.getY() - 1, textPain);
			}
		}

	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			float x = event.getX();
			float y = event.getY();
			Iterator<ImageBean> itr = TestMap.maps.values().iterator();

			ImageBean tmp = null;

			while (itr.hasNext()) {
				tmp = itr.next();
				if (x > tmp.getX() && x < (tmp.getX() + tmp.getWidth())) {
					if (y > tmp.getY() && y < (tmp.getY() + tmp.getHeigth())) {
						// selected = tmp;
						// status = 1;
						// this.isViewOn = true;

						LayerManager.cityLayer.cityPanel.cityInfo=tmp;
						LayerManager.cityLayer.cityPanel.open();
						return true;
					}
				}
			}
		}
		return false;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		status = 0;
		this.isViewOn = true;
		new Thread(this).start();
		Log.i("TAG", "surfaceCreated()");

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.i("TAG", "surfaceChanged()");
		this.isViewOn = false;
	}

	public void run() {
		Canvas canvas;
		try {
			while (flag) {
				while (isViewOn) {
					canvas = null;
					try {
						canvas = holder.lockCanvas(null);
						synchronized (holder) {
							this.onDraw(canvas);
						}
					} finally {
						if (canvas != null) {
							// 更新屏幕显示内容
							holder.unlockCanvasAndPost(canvas);
						}
					}
				}
				Thread.sleep(50);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}
}
